import CCRequest from './request'
import { BASE_URL, TIME_OUT } from './request/config'
import localCache from '@/utils/cache'

const ccRequest = new CCRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      console.log('实例级别请求成功的拦截')
      const token = localCache.getCache('token')
      if (token) {
        // 给请求头添加 token
        config.headers.Authorization = `Bearer ${token}`
      }
      return config
    },
    requestInterceptorCatch: (err) => {
      console.log('实例级别请求失败的拦截')
      return err
    },
    responseInterceptor: (res) => {
      console.log('实例级别响应成功的拦截')
      return res
    },
    responseInterceptorCatch: (err) => {
      console.log('实例级别响应失败的拦截')
      console.log(err)
      return err
    }
  }
})
export default ccRequest
